import classicBeh from '../classic-beh.js';
let mMgr = wx.getBackgroundAudioManager();
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [classicBeh],
  properties: {
    src: String,
    title: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    playing: false,
    pauseSrc: '../../images/player@waitting.png',
    playSrc: '../../images/player@playing.png'
  },

  attached: function() {
    this._recoverStatus();
    this._monitorSwitch();
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onPlay: function(event) {
      if (!this.data.playing) {
        this.setData({playing:true});
        mMgr.src = this.properties.src;
        mMgr.title = this.properties.title;
      } else {
        this.setData({playing:false});
        mMgr.pause();
      }
    },

    // 判断当前音乐页面与播放的音乐是否一致，然后恢复这个音乐页面的状态。
    _recoverStatus:function() {
      if (mMgr.paused) {
        this.setData({playing:false});
        return ;
      }
      if (mMgr.src == this.properties.src) {
        this.setData({playing: true});
      }
    },

    // 手机后台对微信音乐的总控开关的控制，使我们要然这2者的播放暂停状态要控制同步。
    _monitorSwitch: function() {
      mMgr.onPlay(() => {this._recoverStatus()});
      mMgr.onPause(() => {this._recoverStatus()});
      mMgr.onStop(() => {this._recoverStatus()});
      mMgr.onEnded(() => {this._recoverStatus()});
    }
  }
})
